﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using IHISCETeim.Library;
using System.Data.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using IHISCETeim.Model;
namespace IHISCETeim.DAL
{
    public class OProjectDAL
    {
        /// <summary>
        /// 返回-1表示,已存在相同的PNAME
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public static int Add(OProjectInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OProject_Add",obj.MachineType,obj.PName,obj.TypeSuff,obj.StartNum,obj.EndNum,obj.MacNum,obj.Remark,obj.ListMac,obj.OrderIndex,obj.IsComplete,obj.IsShow);
            return Convert.ToInt32(DB.ExecuteScalar(dbCommand));
        }
        public static bool Delete(int PID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OProject_Delete", PID);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch
            {
                return false;
            }
        }
        public static DataTable GetPagination(string MachineType, string PName, int PageIndex, int PageSize, string Sort, ref int TotalCount)
	    {
		    Database DB = DatabaseFactory.CreateDatabase();		
		
            DbCommand dbCommand = DB.GetStoredProcCommand("OProject_GetPagination");
            DB.AddInParameter(dbCommand, "MachineType", DbType.String,MachineType);
            DB.AddInParameter(dbCommand, "PName", DbType.String,PName);
            DB.AddInParameter(dbCommand, "PageIndex", DbType.Int32,PageIndex);
            DB.AddInParameter(dbCommand, "PageSize", DbType.Int32,PageSize);
            DB.AddInParameter(dbCommand, "Sort", DbType.String,Sort);
            DB.AddOutParameter(dbCommand, "TotalCount", DbType.Int32,8);
			DataTable dt=  DB.ExecuteDataSet(dbCommand).Tables[0];
				
            TotalCount=Convert.ToInt32(DB.GetParameterValue(dbCommand,"TotalCount"));
            return dt;
	    }
        public static bool Update(OProjectInfo obj)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OProject_Update", obj.PID, obj.MachineType, obj.PName, obj.TypeSuff, obj.StartNum, obj.EndNum, obj.MacNum, obj.Remark, obj.ListMac, obj.OrderIndex, obj.IsComplete,obj.IsShow);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }

        public static IDataReader GetById(int PID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OProject_GetByID", PID);
            return DB.ExecuteReader(dbCommand);
        }
        /// <summary>
        /// 获取所有未完成的作业项目
        /// </summary>
        /// <returns></returns>
        public static DataTable GetByIsComplete()
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OProject_GetByIsComplete");
            return DB.ExecuteDataSet(dbCommand).Tables[0];
        }
        /// <summary>
        /// 获取需发送通知给生管部的生产项目
        /// </summary>
        /// <returns></returns>
        public static IDataReader GetNeedSendEmail()
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OProject_GetNeedSendEmail");
            return DB.ExecuteReader(dbCommand);
        }
        /// <summary>
        /// 更新已发送邮件标示
        /// </summary>
        /// <param name="PID"></param>
        /// <returns></returns>
        public static bool UpdateSendEmail(int PID)
        {
            Database DB = DatabaseFactory.CreateDatabase();

            DbCommand dbCommand = DB.GetStoredProcCommand("OProject_UpdateSendEmail", PID);
            try
            {
                DB.ExecuteScalar(dbCommand);
                return true;
            }
            catch (Exception exc)
            {
                return false;
            }
        }
    }
}
